"""
进程池
"""
import os
from multiprocessing import Pool
from random import random
import time


# 非阻塞式进程 : 全部太假到队列中 , 立刻返回 , 并没有等待其他进程执行完毕


def fun_task(task_name):
    print("{} -> 开始做任务了".format(task_name))
    start = time.time()
    time.sleep(random() * 2)
    end = time.time()
    return "{} -> 完成任务 , 用时 -> {} , 进程ID -> {}".format(task_name, end - start, os.getpid())


def fun_callback(n):
    print(n)


if __name__ == '__main__':

    pool = Pool(5)

    tasks = ['吃饭', '睡觉', '打豆豆', '听歌', '看剧', '打球', '跑步', '逛街']
    for i in tasks:
        process = pool.apply_async(fun_task, args=(i,), callback=fun_callback)

    pool.close()  # 添加任务结束
    pool.join()  # 让主进程让步

    print("任务全部执行结束")
